election.data <- read.csv("https://raw.githubusercontent.com/VigneshReddy79/STA553-DataViz/main/Datasets/countypresidential_election_2000-2020.csv")
fips.code <- read.csv("https://raw.githubusercontent.com/VigneshReddy79/STA553-DataViz/main/Datasets/fips2geocode.csv")
election.2020 <- election.data %>%
filter( year == 2020 & (party == "DEMOCRAT" | party == "REPUBLICAN") ) %>%
mutate( votes.percent = round((candidatevotes/totalvotes)*100, 2) )
election.final <- election.2020 %>%
group_by( state_po, county_fips) %>%
summarise( highest.votespercent = max(votes.percent, na.rm = TRUE) )
election.final <- merge( election.2020, election.final, by = "county_fips", all.x = FALSE)
election.final <- election.final %>%
filter( votes.percent == highest.votespercent )
election_fips <- merge(election.final, fips.code, by.x = "county_fips", by.y = "fips", all.x = FALSE)
election_fips <- election_fips %>%
mutate( republican = ifelse(party == "REPUBLICAN", yes = 1, no = 0)) %>%
mutate(state = state_po.x, party_won = party) %>%
select(state, county_fips, county_name, party_won, candidatevotes, totalvotes, votes.percent, republican, lat, lon)
election_fips$county_fips <- ifelse ((election_fips$county_fips < 10000),
yes = paste(0,election_fips$county_fips, sep = ""), no = election_fips$county_fips)
saveRDS(election_fips, file="D:/OneDrive - West Chester University of PA/Documents/Spring'22/STA553-DataViz/git/Datasets/election_fips.RData")
url <- "https://github.com/pengdsci/sta553/raw/main/data/geojson-counties-fips.json"
counties <- rjson::fromJSON(file=url)
g <- list(scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white'))
fig <- plot_ly() %>%
add_trace( type = "choropleth",
geojson = counties,
locations = election_fips$county_fips,
z = election_fips$republican,
colors = c("blue","red"),
zmin = 0,
zmax = 1,
text = ~paste("<br>County: ", election_fips$county_name,
"<br>Party won: ", election_fips$party,
"<br>Winner votes percent: ", election_fips$votes.percent,
"<br>Total votes: ", election_fips$totalvotes,
"<br>State: ", election_fips$state),
hoverinfo = "text",
marker = list(line=list(width=0.3))) %>%
hide_colorbar() %>%
layout( title = list(text = "<b>US Presidential elections 2020 by County</b>",
font = list(size = 20,
color = "darkred")),
margin = list( b = 15, l = 25, t = 85, r = 25),
geo = g)
fig
library("RColorBrewer")
fig2 <- plot_ly() %>%
add_trace( type = "choropleth",
geojson = counties,
locations = election_fips$county_fips,
z = election_fips$votes.percent,
colorscale = "TealGrn",
zmin = 0,
zmax = 100,
text = ~paste("<br>County: ", election_fips$county_name,
"<br>Party won: ", election_fips$party,
"<br>Winner votes percent: ", election_fips$votes.percent,
"<br>Total votes: ", election_fips$totalvotes,
"<br>State: ", election_fips$state),
hoverinfo = "text",
marker = list(line=list(width=0.3))) %>%
colorbar(title = "Winner Votes percent(%)") %>%
layout( title = list(text = "<b>US Presidential elections 2020 by County</b>",
font = list(size = 20,
color = "darkred")),
margin = list( b = 15, l = 25, t = 85, r = 25),
geo = g)
fig2